package com.cg.slms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cg.slms.domain.Client;
import com.cg.slms.service.IClientService;

@Controller
public class ClientController {
	@Resource(name="clientService")
	private IClientService clientService;
	/* 员工档案查询 */
	@RequestMapping("/clientAction")
	public ModelAndView findClient(Client client) {
		ModelAndView modelAndView;
		List<Client> clients = clientService.findClient(client);
		if(clients!=null){
			modelAndView=new ModelAndView("/archive/client");
			modelAndView.addObject("clients", clients) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	//添加控制器
	@RequestMapping("/addClientAction")
	public ModelAndView addClient(@Valid Client client,Errors errors) {
			ModelAndView modelAndView;
			if (errors.getErrorCount()>0){//客户信息出现问题
				modelAndView=new ModelAndView("/archive/addClient");
				return modelAndView;
			}
			boolean ret = clientService.addClient(client);
			if(ret){
				String finish_Url="/slms2/moduls/archive/client.jsp";
				modelAndView=new ModelAndView("forward:/finish.jsp");
				modelAndView.addObject("finish_Url", finish_Url) ;
			}else{
				modelAndView=new ModelAndView("forward:/error.jsp");//写入数据库出现问题
			}
			return modelAndView;
		}
	//找到id控制器
		@RequestMapping("/findClientByIdAction")
		public ModelAndView findClientById(String id) {
			System.out.print("你好"+id);
			ModelAndView modelAndView;
			Client client= clientService.findClientById(id);
			if(client!=null){
				modelAndView=new ModelAndView("/archive/updateClient");
				modelAndView.addObject("client",client) ;
			}else{
				modelAndView=new ModelAndView("forward:/error.jsp");
			}
			return modelAndView;
		}
		//修改控制器
		@RequestMapping("/updateClientAction")
		public ModelAndView updateClient(@Valid Client client,Errors errors) {
			ModelAndView modelAndView;
			if (errors.getErrorCount()>0){
				modelAndView=new ModelAndView("/archive/updateClient");
				return modelAndView;
			}
			boolean ret = clientService.updateClient(client);
			if(ret){
				String finish_Url="/slms2/moduls/archive/client.jsp";
				modelAndView=new ModelAndView("forward:/finish.jsp");
				modelAndView.addObject("finish_Url", finish_Url) ;
			}else{
				modelAndView=new ModelAndView("forward:/error.jsp");
			}
			return modelAndView;
		}
		//删除
		@RequestMapping("/deleteClientByIdAction")
		public String deleteClientById(String id) {
			boolean ret= clientService.deleteClientById(id);
			if(ret){
				return "forward:/clientAction";
			}else{
				return "forward:/error.jsp";
			}
		}

}
